<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form ref="ruleForm" label-width="220px" :model="userInfo" :rules="rules">
      <!--手机 -->

      <el-form-item label="手机" prop="mobile">
        <el-input v-model="userInfo.mobile" style="width:220px" />
      </el-form-item>

      <!-- 工号 入职时间 -->

      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker
          v-model="userInfo.timeOfEntry"
          type="date"
          class="inputW"
          value-format="yyyy-MM-dd"
        />
      </el-form-item>

      <!-- 员工照片 -->

      <el-form-item label="员工头像">
        <!-- 放置上传图片 -->
        <uploadImg v-model="userInfo.staffPhoto" />
      </el-form-item>

      <!-- 保存个人信息 -->
      <el-form-item>
        <el-button type="primary" @click="doSave">保存更新</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { getEmployeesInfo, saveEmployeesInfo } from '@/api/employees'
export default {
  data() {
    return {
      userId: this.$route.query.id,
      userInfo: {
        mobile: '',
        timeOfEntry: '',
        staffPhoto: 'https://foruda.gitee.com/avatar/1661822187734334827/11531799_yy0108_1661822187.png'
      },
      rules: {
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' }
        ],
        timeOfEntry: [
          { required: true, message: '入职时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.loadEmployessInfo()
  },
  methods: {
    async loadEmployessInfo() {
      const res = await getEmployeesInfo(this.userId)
      this.userInfo = res.data
    },
    // 调用保存员工信息
    doSave() {
      this.saveUpdate()
    },
    // 保存员工信息
    saveUpdate() {
      this.$refs.ruleForm.validate(async(valid) => {
        if (valid) {
          try {
            const res = await saveEmployeesInfo(this.userInfo)
            this.$message.success(res.message)
          } catch (err) {
            this.$message.error(err.message)
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
  .user-info{
    padding-top:20px;
  }
</style>
